package com.beagle.njddapp.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.ZdBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.adapter.ZdListAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.SuggestPresenter;
import com.beagle.njddapp.ui.view.SuggestView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-27 14:22
 */
public class SuggestActivity extends BaseMvpActivity<SuggestView, SuggestPresenter> implements SuggestView, RecyclerViewItemClickLisenter<ZdBean> {


    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.ed_input)
    EditText ed;
    @BindView(R.id.tv_ques)
    TextView tvQues;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.rl_order)
    RelativeLayout rlOrder;
    @BindView(R.id.ques_list)
    RecyclerView quesLst;
    @BindView(R.id.num_list)
    ListView numList;
    @BindView(R.id.tv_num)
    TextView tvNUm;
    private List<ZdBean> zdBeanList;
    private String type,num;
    private ZdListAdapter zdListAdapter;
    private List<String> stringList;
    private ArrayAdapter arrayAdapter;



    @Override
    public int getLayoutId() {
        return R.layout.activity_suggest;
    }

    @Override
    public void init() {
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        container.setBackgroundColor(Color.WHITE);
        tvTitle.setText("意见反馈");
        zdBeanList=new ArrayList<>();
        quesLst.setLayoutManager(new LinearLayoutManager(this));
        presenter.queryDictList();
        stringList=new ArrayList<>();
        presenter.getList();
        numList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                tvNUm.setText(stringList.get(i));
                num=stringList.get(i);
                numList.setVisibility(View.GONE);
            }
        });
    }


    @OnClick({R.id.common_back_iv,R.id.tv_commit,R.id.tv_ques,R.id.rl_order})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_ques:
                if (zdBeanList.size()>0){
                    if ( zdListAdapter==null){
                        zdListAdapter=new ZdListAdapter(zdBeanList,this);
                        zdListAdapter.setOnItemClickLisenter(this);
                        quesLst.setAdapter(zdListAdapter);
                        quesLst.setVisibility(View.VISIBLE);
                    }else {
                        quesLst.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case R.id.rl_order:
                if (stringList.size()>0){
                    if (arrayAdapter==null){
                        arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,stringList);
                        numList.setAdapter(arrayAdapter);
                    }
                    numList.setVisibility(View.VISIBLE);
                }else {
                    ToastUtils.showShort("暂无加油订单");
                }
                break;

            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.tv_commit :
                if (TextUtils.isEmpty(ed.getText().toString())){
                    ToastUtils.showShort("请正确输入");
                    return;
                }
                presenter.commit(ed.getText().toString(),type,num);
                break;
        }
    }

    @Override
    public SuggestView getMvpView() {
        return this;
    }

    @Override
    public SuggestPresenter createPresenter() {
        return new SuggestPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);

    }
    @Override
    public void showZd(List<ZdBean> items) {
        zdBeanList.addAll(items);
        if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
            zdBeanList.remove(0);
            rlOrder.setVisibility(View.GONE);
        }
        if (zdBeanList!=null&&zdBeanList.size()>0){
            tvQues.setText(zdBeanList.get(0).getName());
            type=zdBeanList.get(0).getValue();
        }
    }
    @Override
    public void showSuccess() {
        ToastUtils.showShort("您的问题已提交");
        tvTitle.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        },2000);
    }

    @Override
    public void showOrders(List<OilOrderBean> results) {
        if (results!=null&&results.size()>0){
            for (OilOrderBean oilOrderBean:results){
                stringList.add(oilOrderBean.getOrderNum());
            }
        }
    }

    @Override
    public void showOrderList(List<OilHomeBean> results) {
        if (results!=null&&results.size()>0){
            for (OilHomeBean oilOrderBean:results){
                stringList.add(oilOrderBean.getOrderNum());
            }
        }
    }

    @Override
    public void onItemClickLisenter(ZdBean zdBean) {
        tvQues.setText(zdBean.getName());
        type=zdBean.getValue();
        if ("加油订单问题".equals(zdBean.getName())){
            rlOrder.setVisibility(View.VISIBLE);
        }else {
            rlOrder.setVisibility(View.GONE);
        }
        if (quesLst.getVisibility()==View.VISIBLE){
            quesLst.setVisibility(View.GONE);
        }
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
////        if (quesLst.getVisibility()==View.VISIBLE){
////            quesLst.setVisibility(View.GONE);
////            return true;
////        }
////        if (numList.getVisibility()==View.VISIBLE){
////            numList.setVisibility(View.GONE);
////            return true;
////        }
//        return super.dispatchTouchEvent(ev);
//    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (quesLst.getVisibility()==View.VISIBLE){
            quesLst.setVisibility(View.GONE);
            return true;
        }
        if (numList.getVisibility()==View.VISIBLE){
            numList.setVisibility(View.GONE);
            return true;
        }
        return super.onTouchEvent(event);
    }
}
